package com.serkol.utils;

import cn.hutool.core.util.RandomUtil;
import org.apache.commons.lang3.RandomStringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class OrderUtils {
    public static String getOrderNum(Long userId) {
        //时间（精确到毫秒）
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyMMddHHmm");
        String localDate = LocalDateTime.now().format(ofPattern);
        //3位随机数
        String randomNumeric =String.valueOf(RandomUtil.randomInt(0,9));
        //5位用户id
        int subStrLength = 5;
        String sUserId = userId.toString();
        int length = sUserId.length();
        String str;
        if (length >= subStrLength) {
            str = sUserId.substring(length - subStrLength, length);
        } else {
            str = String.format("%0" + subStrLength + "d", userId);
        }
        System.out.println(localDate+"---Len:"+localDate.length());
        System.out.println(randomNumeric+"---Len:"+randomNumeric.length());
        System.out.println(str+"---Len:"+str.length());
        String orderNum = localDate + randomNumeric + str;
        System.out.println(orderNum+"---len:"+orderNum.length());
        //    log.info("订单号:{}", orderNum);
        return orderNum;
    }

    public static String getInvalidCode(long userId) {
        //时间（精确到毫秒）
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyHd");
        String localDate = LocalDateTime.now().format(ofPattern);
        //3位随机数
        String randomNumeric = RandomStringUtils.randomNumeric(3);
        //5位用户id
        int subStrLength = 5;


        String orderNum = localDate + randomNumeric+userId;
        //    log.info("订单号:{}", orderNum);
     //   System.out.println(orderNum);
        return orderNum;
    }
}
